package cn.wetcland.urban.admin.service.impl;

import cn.wetcland.urban.admin.dto.ArticleAddDTO;
import cn.wetcland.urban.admin.dto.ArticleUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtArticleMapper;
import cn.wetcland.urban.admin.service.UtArticleService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.generator.domain.UtArticle;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author hongyao.tan
* @description 针对表【ut_article】的数据库操作Service实现
* @createDate 2024-03-07 17:17:41
*/
@Service
public class UtArticleServiceImpl extends ServiceImpl<UtArticleMapper, UtArticle> implements UtArticleService {

    @Resource
    private UtArticleMapper articleMapper;

    @Override
    public Page<UtArticle> articleList(BaseConditionSearchDTO searchDTO) {
        Page<UtArticle> utArticlePage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return articleMapper.selectArticlePageList(utArticlePage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void articleUpdate(ArticleUpdateDTO updateDTO) {
        UtArticle utArticle = articleMapper.selectById(updateDTO.getArticleId());
        MyAssert.notNull(utArticle, "该文章不存在");
        CopyObjectUtil.copyPropertiesIgnoreNull(updateDTO, utArticle);
        articleMapper.updateById(utArticle);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void articleDelete(Long articleId) {
        articleMapper.deleteById(articleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void articleAdd(ArticleAddDTO addDTO) {
        UtArticle utArticle = new UtArticle();
        CopyObjectUtil.copyPropertiesIgnoreNull(addDTO, utArticle);
        articleMapper.insert(utArticle);
    }
}




